var mobile="";

$(document).ready(function () {
$.jgrid.defaults.styleUI = "Bootstrap";
$("#table_list_1").jqGrid({
    data: [],
    datatype: "local",
    height: "100%",
    autowidth: true,
    shrinkToFit: true,
    rowNum: 20,
    rowList: [10, 20, 30,60,90,100,200,500],
    //colNames: ["编号", "名称", "手机号码"],
    colModel: [ {
        name: 'id', index: 'id', align:"center",width: 30, label: "编号", align: 'center',
    },
    {
        name: 'companyid', index: 'company_id', align:"center",width: 25, label: "companyid", align: 'center',hidden:true,
    },
    
    {
        name: 'devicename', index: 'devicename',align:"center", width: 30, label: "车牌号", align: 'center',
    },
    {
        name: 'devicenumber', index: 'devicenumber',align:"center", width: 30, label: "设备号", align: 'center',
    },
    
    {
        name: 'onlinetime', index: 'onlinetime',align:"center", width: 30, label: "播放开始日期", align: 'center',
        formatter: function (value, options, row) {
           
            if(typeof(value)!="undefined"){
                try{
             var date = new Date(value);
                
             return date.Format("yyyy-MM-dd hh:mm");
                }catch(ex){
 
                 console.log(ex);
                }
            }
            return "";
         }
    },
    {
        name: 'offlinetime', index: 'offlinetime',align:"center", width: 30, label: "播放结束日期", align: 'center',
        formatter: function (value, options, row) {
           
            if(typeof(value)!="undefined"){
                try{
             var date = new Date(value);
                
             return date.Format("yyyy-MM-dd hh:mm");
                }catch(ex){
 
                 console.log(ex);
                }
            }
            return "";
         }
    },
    {
        name: 'profit', index: 'profit', align:"center",width: 30, label: "盈利", align: 'center',

    },
],
    pager: "#pager_list_1",
    viewrecords: true,
    multiselect:true,
    caption: "上线日志",
    hidegrid: false,
    ondblClickRow:function(row){
    $("#update").click();
    modify(row);
      // $('#myModal').modal('show')
    },
    onPaging:function(pageBtn){
        console.log(":"+pageBtn); 
//	    	var re_records = $jqgrid.getGridParam('records');//获取返回的记录数  
        var re_page =  $("#table_list_1").getGridParam('page');//获取返回的当前页
        var re_rowNum=  $("#table_list_1").getGridParam('rowNum');//获取每页数
        var re_total=  $("#table_list_1").getGridParam('lastpage');//获取总页数
        //alert(re_total);
        
        if(pageBtn==="next"){
           // initJqgrid(re_page+1,re_rowNum);
          // if()
           pageval=pageval+1;
           cxlist();
        }
        if(pageBtn==="prev"){
            
            pageval=pageval-1;  cxlist();
        }

        if(pageBtn=="last"){
  
            pageval=re_total;
          
            cxlist();

        }
        if(pageBtn=="first"){
  
            pageval=1;
          
            cxlist();

        }
        
        if(pageBtn=="records"){

            sizeval=$(".ui-pg-selbox").val();
            pageval=1;
            cxlist();
        }
    },

});
$("#table_list_1").setGridHeight($(window).height() * 0.73);

 $(window).resize(function(){
 $("#table_list_1").setGridWidth($(window).width());$("#table_list_1").setGridHeight($(window).height() * 0.73);
});
});
var sizeval=20;
var pageval=1;
var usernameval=$("#usnameval").val();
var conameval="";
var json="";



window.cxlist=function(){
     
    var startTime=$("#demo_datetime").val();
    var endTime=$("#demo_endtime").val();
    domain.ajaxUrl({
        method: 'POST',
        url: domain.testUrl+"/Threeinjection/user/queryOnLineLogPaging",
        data: {size:sizeval,page:pageval,coId:sessionStorage.getItem("coid"),sessionid:sessionStorage.getItem("sid"),
        deviceName:$("#carNumber").val(),onlineTime:startTime,offlineTime:endTime},
       // datatype:"JSON",
        success: function (response) {
          
          // console.log(response);
             
            json=response;
            var data=JSON.parse(response);
            var reader = {
                root: function (obj) { return data.rows; },
                page: function (obj) { return pageval; },
                total: function (obj) { return data.taotal; },
                records: function (obj) {

                    return data.records;
                },
               
            };
           
         
                   
            $("#table_list_1").setGridParam({ data: data.rows, localReader: reader }).trigger('reloadGrid');
            var re_records = $("#table_list_1").getGridParam('records');
            if(re_records==0||re_records==null){
                if($(".norecords").html() == null){$("#usnameval").val("");
                    $("#table_list_1").parent().append("<div class=\"norecords\">没有符合数据</div>");
                }
                $(".norecords").show();
            }else{

                $(".norecords").hide();
            }
           if(sessionStorage.getItem("isParent")!="true"){
           $(".ui-row-ltr").each(function(){
             if($(this).find("td").eq(2).text()!==sessionStorage.getItem("coid")){
                $(this).hide();
             }
           });
        }

        var totalProfit=0;
        var rowData = $('#table_list_1').jqGrid("getRowData");//所有行，数组
       
            for(var i=0; i<rowData.length; i++){    
                
                totalProfit+=parseInt(rowData[i].profit);//总盈利
            }
        
           $("#totalProfit").val(totalProfit);
    
           
        }
    });
}

Date.prototype.Format = function (fmt) {
    //author:wangweizhen
    var o = {
        "M+": this.getMonth() + 1,                 //月份   
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时   
        "m+": this.getMinutes(),                 //分   
        "s+": this.getSeconds(),                 //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}



 $(function(){

    // $('button:eq(1)').click(function () {
      
    //     $("#demo_datetime").val("4564");
       
    //     $("#demo_endtime").val("");
    // });
    //  $("#demo_datetime,#demo_endtime").attr({"readonly":false},{"disabled":false});
    $("button").eq(0).click(function(){
    if($("#demo_datetime").val()!=""&& $("#demo_endtime").val()!=""){
        if($("#demo_datetime").val()>$("#demo_endtime").val()){
            swal("OMG!", "开始时间不能大于结束时间", "error");
            return;
        } 
    }
        sizeval=20;
        pageval=1;
        cxlist();
    });
  
    cxlist();//加载

});

